//
//  UserRewardBannerCell.swift
//  koolcar
//
//  Created by xiantian on 2023/11/1.
//

import UIKit

class UserRewardBannerView: UIView {
    var bkgView: UIView!
    var bkgImgView: UIImageView!
    var bkgLayer: CAGradientLayer!
    
    var vipLab: UILabel!
    var vipImgBtn: UIButton!
    var pointLab: UILabel!
    
    var productDisLab: UILabel!
    var serviceDisLab: UILabel!
    var userData: UserInfoDataModel?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        bkgView = UIView()
        self.addSubview(bkgView)
        
        vipImgBtn = UIButton()
        vipImgBtn.setup_backgroundImage(image: UIImage.init(named: "reward_arrow"))
        vipImgBtn.setup_titleLabel(text: "LV")
        vipImgBtn.setup_titleColor(color: UIColor.init(hex: 0x825B1D))
        vipImgBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        bkgView.addSubview(vipImgBtn)
        
        vipLab = UILabel()
        vipLab.textColor = UIColor.init(hex: 0x825B1D)
        vipLab.backgroundColor = UIColor.clear
        vipLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 16)
        vipLab.numberOfLines = 1
        vipLab.lineBreakMode = .byTruncatingTail
        vipLab.textAlignment = .left
        //vipLab.text = "普通会员"
        bkgView.addSubview(vipLab)
        
        pointLab = UILabel()
        pointLab.textColor = UIColor.init(hex: 0x825B1D)
        pointLab.backgroundColor = UIColor.clear
        pointLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 13)
        pointLab.numberOfLines = 1
        pointLab.lineBreakMode = .byTruncatingTail
        pointLab.textAlignment = .right
        //pointLab.text = "12,800"
        bkgView.addSubview(pointLab)
        
        productDisLab = UILabel()
        productDisLab.textColor = UIColor.init(hex: 0xE2CA8A)
        productDisLab.backgroundColor = UIColor.clear
        productDisLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 13)
        productDisLab.numberOfLines = 1
        productDisLab.lineBreakMode = .byTruncatingTail
        productDisLab.textAlignment = .center
        //productDisLab.text = "商品 | 八折"
        productDisLab.layer.borderWidth = 1
        productDisLab.layer.cornerRadius = 14
        productDisLab.layer.masksToBounds = true
        productDisLab.layer.borderColor = UIColor.init(hex: 0xE2CA8A).cgColor
        productDisLab.isHidden = true
        bkgView.addSubview(productDisLab)
        
        serviceDisLab = UILabel()
        serviceDisLab.textColor = UIColor.init(hex: 0xE2CA8A)
        serviceDisLab.backgroundColor = UIColor.clear
        serviceDisLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 13)
        serviceDisLab.numberOfLines = 1
        serviceDisLab.lineBreakMode = .byTruncatingTail
        serviceDisLab.textAlignment = .center
        //serviceDisLab.text = "服务 | 八折"
        serviceDisLab.layer.borderWidth = 1
        serviceDisLab.layer.cornerRadius = 14
        serviceDisLab.layer.masksToBounds = true
        serviceDisLab.isHidden = true
        serviceDisLab.layer.borderColor = UIColor.init(hex: 0xE2CA8A).cgColor
        bkgView.addSubview(serviceDisLab)
        
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        bkgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        vipImgBtn.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.top.equalTo(20)
            make.width.equalTo(40)
            make.height.equalTo(20)
        }
        
        vipLab.snp.makeConstraints { make in
            make.left.equalTo(vipImgBtn.snp.right)
            make.top.equalTo(20)
            make.height.greaterThanOrEqualTo(20)
        }
        
        pointLab.snp.makeConstraints { make in
            make.right.equalTo(-14)
            make.top.equalTo(20)
            make.left.equalTo(vipLab.snp.right)
            make.height.greaterThanOrEqualTo(20)
        }
        
        productDisLab.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.top.equalTo(vipLab.snp.bottom).offset(18)
            make.height.equalTo(28)
            make.width.equalTo(140)
        }
        
        serviceDisLab.snp.makeConstraints { make in
            make.right.equalTo(-14)
            make.top.equalTo(productDisLab.snp.top)
            make.height.equalTo(28)
            make.width.equalTo(140)
        }
        
        self.layoutIfNeeded()
        //self.createBannerLayer(isVip: false)
    }
    
    public func setUserBannerView(data: UserInfoDataModel) {
        self.userData = data
        self.vipLab.text = data.memberLevelVO?.levelName ?? ""
        self.pointLab.text = String(format: "%.0f", data.integralValue ?? 0)
        if pointLab.text == "0" {
            pointLab.isHidden = true
        }else{
            pointLab.isHidden = false
        }
        let dis = MMLocalizedString("pcuser_off", comment: "折")
        
        let productPrice = self.willShowProductDisLab()
        if productPrice != "" {
            self.productDisLab.layer.borderWidth = 1
            self.productDisLab.isHidden = false
            let temp1 = String(format: "%@%@", productPrice, dis)
            self.productDisLab.text = String(format: "%@ | %@",
                                             MMLocalizedString("suer_reward_product", comment: "商品"),
                                             temp1)
        }else{
            self.productDisLab.isHidden = true
            self.productDisLab.layer.borderWidth = 0
        }
        
        
        let serverPrice = self.willShowServiceDisLab()
        if serverPrice != "" {
            self.serviceDisLab.layer.borderWidth = 1
            self.serviceDisLab.isHidden = false
            let temp2 = String(format: "%@%@", serverPrice, dis)
            self.serviceDisLab.text = String(format: "%@ | %@",
                                             MMLocalizedString("suer_reward_server", comment: "服务"),
                                             temp2)
        }else{
            self.serviceDisLab.isHidden = true
            self.serviceDisLab.layer.borderWidth = 0
        }
        
        
        if (data.levelValue ?? 0) > 0 {
            //vip
            self.createBannerLayer(isVip: true)
        }else{
            //非vip
            self.createBannerLayer(isVip: false)
        }
    }
    
    private func willShowProductDisLab() -> String {
        var tempStr = ""
        let appLan = XTManageCenter.getAppleLanguages()
        let flag = self.userData?.memberLevelVO?.productFlag ?? 0
        if flag == 0 {
            return tempStr
        }
        //展示
        if appLan == AppLanguage.english.rawValue {
            if let dis = self.userData?.memberLevelVO?.productDiscount {
                tempStr = String(format: "%.0f", dis)
            }
        }else{
            if let dis = self.userData?.memberLevelVO?.productStr {
                tempStr = String(format: "%@", dis)
            }
        }
        return tempStr
    }
    
    private func willShowServiceDisLab() -> String {
        var tempStr = ""
        let appLan = XTManageCenter.getAppleLanguages()
        let flag = self.userData?.memberLevelVO?.serverFlag ?? 0
        if flag == 0 {
            return tempStr
        }
        //展示
        if appLan == AppLanguage.english.rawValue {
            if let dis = self.userData?.memberLevelVO?.serverDiscount{
                tempStr = String(format: "%.0f", dis)
            }
        }else{
            if let dis = self.userData?.memberLevelVO?.serverStr{
                tempStr = String(format: "%@", dis)
            }
        }
        return tempStr
    }
    
    private func createBannerLayer(isVip: Bool) {
        if self.bkgImgView != nil {
            self.bkgImgView.removeFromSuperview()
            self.bkgImgView = nil
        }
        if self.bkgLayer != nil {
            self.bkgLayer.removeFromSuperlayer()
            self.bkgLayer = nil
        }
        if isVip {
            bkgImgView = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: bkgView.frame.size.width, height: bkgView.frame.size.height))
            bkgImgView.image = UIImage.init(named: "reward_banner_black")
            bkgView.insertSubview(bkgImgView, at: 0)
            self.productDisLab.isHidden = false
            self.serviceDisLab.isHidden = false
            self.vipLab.textColor = UIColor.init(hex: 0xE2CA8A)
            self.pointLab.textColor = UIColor.init(hex: 0xE2CA8A)
            self.productDisLab.textColor = UIColor.init(hex: 0xE2CA8A)
            self.serviceDisLab.textColor = UIColor.init(hex: 0xE2CA8A)
        }else{
            self.productDisLab.isHidden = true
            self.serviceDisLab.isHidden = true
            self.vipLab.textColor = UIColor.init(hex: 0x825B1D)
            self.pointLab.textColor = UIColor.init(hex: 0x825B1D)

            let bound = CGRect(x: 0, y: 0, width: bkgView.frame.size.width, height: bkgView.frame.size.height)
            let bgLayer1 = CAGradientLayer()
            bgLayer1.colors = [UIColor(red: 0.98, green: 0.87, blue: 0.67, alpha: 1).cgColor, UIColor(red: 0.95, green: 0.78, blue: 0.53, alpha: 1).cgColor]
            bgLayer1.locations = [0, 1]
            bgLayer1.frame = bound
            bgLayer1.startPoint = CGPoint(x: 0.5, y: 0)
            bgLayer1.endPoint = CGPoint(x: 1, y: 1)
            self.bkgLayer = bgLayer1
            bkgView.layer.insertSublayer(self.bkgLayer, at: 0)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
